package Java实训.第七天字符流;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class Tast4 {
    /*
     * 统计”C:\Users\gdq\OneDrive\桌面\File文件夹\task-20221125.doc“文件中
     * 不同职位的平均薪资
     */
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\gdq\\OneDrive\\桌面\\File文件夹\\Task4.txt"));
        FileWriter fw = new FileWriter("C:\\Users\\gdq\\OneDrive\\桌面\\File文件夹\\result.txt");
        HashMap<String, Integer> map = new HashMap<>();
        cc(br, map);
        Set<Map.Entry<String, Integer>> set = map.entrySet();
        for (Map.Entry<String, Integer> s : set) {
            fw.write(s.getKey() + ": " + s.getValue() + "\n");
        }
        br.close();
        fw.close();
    }

    public static void cc(BufferedReader br, HashMap<String, Integer> map) throws IOException {
        String str;
        while ((str = br.readLine()) != null) {
            String[] split = str.split(",");
            String str1;
            if (split[4].contains(" 元")) {
                str1 = split[4].replace(" 元", "");
            } else {
                str1 = split[4];
            }
            int xz = (Integer.parseInt(str1.split("-")[1]) + Integer.parseInt(str1.split("-")[0])) / 2;
            if (map.containsKey(str.split(",")[1])){
                int value = map.get(str.split(",")[1]);
                value = (value + xz) / 2;
                map.put(str.split(",")[1], value);
            }else {
                map.put(str.split(",")[1], xz);
            }
        }
    }
}
